package network;

import java.io.*;
import java.net.*;
import java.util.StringTokenizer;


public class Service implements Runnable {
	
	private Server server;
	private Socket clientSocket;
	private MenagerServer menager;
	
	private BufferedReader input;
	private PrintWriter output;
	
	public Service(Socket clientSocket, Server server) {
	    this.server = server;
	    this.clientSocket = clientSocket;
	    menager = new MenagerServer();
	}
	
	void init() throws IOException {
	    Reader reader = new InputStreamReader(clientSocket.getInputStream());
	    input = new BufferedReader(reader);
	    output = new PrintWriter(clientSocket.getOutputStream(), true);
	}
	
	void close() {
	    try {
	        output.close();
	        input.close();
	        clientSocket.close();
	    } catch (IOException e) {
	    } finally {
	        output = null;
	        input = null;
	        clientSocket = null;
	    }
	}
	
	
	public void run() {
	    while (true) {
	        String request = receive();
	        StringTokenizer st = new StringTokenizer(request);
	        String command = st.nextToken();
	        if (command.equals(Protocol.KE_ZALOGUJ)) {
	            send(Protocol.KE_ZALOGOWALEM);
	        } 
	        else if (command.equals(Protocol.PARAM_JAKIE_PREDKOSCI)) {
	        	send(Protocol.PARAM_PREDKOSCI_TO + menager.predkosci());	     
	        } 
	        else if (command.equals(Protocol.PARAM_ILE_ZYC)) {
	        	send(Protocol.PARAM_ZYCIA_TO + menager.iloscZyc());
	        } 
	        else if (command.equals(Protocol.TOP_LISTA_JAKA)) {
	        	send(Protocol.TOP_LISTA_TO + menager.topLista());
	        } 
	        else if (command.equals(Protocol.TOP_ZGLASZAM_WYNIK )) {	        	
	            send(Protocol.TOP_AKCEPTUJE + menager.czyDobryWynik(Integer.parseInt(st.nextToken())));
	        } 
	        else if (command.equals(Protocol.TOP_DANE_ZAAKCEPTOWANEGO)) {
	        	String imie = st.nextToken();
	        	String wynik = st.nextToken();
	        	menager.dodajPozycja(Integer.parseInt(wynik), imie);
	        }
	        else if (command.equals(Protocol.MAPA_DAJ_POZIOM)) {
	        	send(Protocol.MAPA_DAJE_POZIOM + menager.mapa(Integer.parseInt(st.nextToken())));
	        }
	        else if (command.equals(Protocol.MAPA_JAKA_POZYCJA_KE )) {
	            send(Protocol.MAPA_POZYCJA_KE + menager.kePozycja(Integer.parseInt(st.nextToken())));
	        } 
	        else if (command.equals(Protocol.MAPA_WRODZY_ILE_GDZIE)) {
	        	send(Protocol.MAPA_WRODZY_INFO + menager.wrogPozycja(Integer.parseInt(st.nextToken())));
	        }
	        else if (command.equals(Protocol.MAPA_JAKI_CZAS)) {
	        	send(Protocol.MAPA_CZAS + menager.czas(Integer.parseInt(st.nextToken())));
	        }
	        else if (command.equals(Protocol.PUNKTACJA_JAKA )) {
	            send(Protocol.PUNKTACJA_TO + menager.punktacja());
	        } 
	        else if (command.equals(Protocol.KE_WYLOGUJ)) {
	        	send(Protocol.KE_WYLOGOWALEM);
	        	break;
	        }
	        else if (command.equals(Protocol.KE_PRZYJALEM_KONIEC)) {
	        	break;
	        }
	        else if (command.equals(Protocol.NULLCOMMAND)) {
	        	break;
	        }
	    } 
	    
	    server.removeClientService(this);
	}
	
	
	
	void send(String command) {
	    output.println(command);
	    System.out.println("S: " + command);
	}
	
	
	private String receive() {
	    try {
	        return input.readLine();
	    } catch (IOException e) {
	    }
	    return Protocol.NULLCOMMAND;
	} 
	
}
